package de.tabuma.pm.externalproject.controller;

import de.tabuma.pm.*;
import de.tabuma.lib.tabumacontroller;
import de.tabuma.pm.externalproject.entity.*;
import de.tabuma.pm.externalproject.logik.*;
import java.io.*;
import java.util.*;
import java.util.logging.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.google.appengine.api.users.*;

/**
 *
 * @author alefen
 */
public class create extends tabumacontroller {

    private static final Logger log = Logger.getLogger(create.class.getName());

    public create(String servlet, Hashtable parameter) {
        super(servlet, parameter);
    }

    @Override
    public void navigate(ServletContext context, HttpServletRequest request, HttpServletResponse response, PrintWriter out) {

        String wrongUrl = "Url, Parameter or Name is not valid.";

        UserService userService = UserServiceFactory.getUserService();
        User user = userService.getCurrentUser();

        if(request.getParameter(constants.cancel)!=null) {
            list list = new list(this.getServlet(), this.getParameter());
            list.navigate(context, request, response, out);
            return;
        }

        if(request.getParameter(constants.addExternalProject)!=null) {
            request.setAttribute(constants.addExternalProject, "true");
            request.setAttribute(constants.externalProjectName, request.getParameter(constants.externalProjectName));
            request.setAttribute(constants.externalProjectParameter, request.getParameter(constants.externalProjectParameter));
            return;
        }
        if(request.getParameter(constants.addExternalProject1)!=null) {
            String url = request.getParameter(constants.externalProjectUrl);
            String projectName = request.getParameter(constants.externalProjectName);
            String projectParameter = request.getParameter(constants.externalProjectParameter);
            String projectPost = request.getParameter(constants.externalProjectPost);
            List<HiddenParameter> paramList1 = new ArrayList<HiddenParameter>();
            List<LoginUserParameter> paramList2 = new ArrayList<LoginUserParameter>();

            try {

                String projectId = request.getParameter(constants.projectId);
                int projectIdInt = Integer.valueOf(projectId).intValue();

                String parameterList[] = projectParameter.split(",");
                if(parameterList.length<2) {
                    throw new Exception();
                }

                paramList1 = getHiddenParameterList(projectParameter);
                paramList2 = getLoginUserParameterList(projectParameter);

                ExternalProject externalProject = new ExternalProject();
                externalProject.setLoginUrl(url);
                externalProject.setProjectId(projectIdInt);
                externalProject.setName(projectName);
                if(projectPost.equals(constants.externalProjectPost_Post)) {
                    externalProject.setPost(Boolean.TRUE);
                }
                else {
                    externalProject.setPost(Boolean.FALSE);
                }
                int id = ExternalProjectFacade.create(externalProject, user.getEmail());
                for(int i=0;i<paramList1.size();i++) {
                    HiddenParameter hp = paramList1.get(i);
                    hp.setExternalprojectId(id);
                    HiddenParameterFacade.create(hp, user.getEmail());
                }
                for(int i=0;i<paramList2.size();i++) {
                    LoginUserParameter lup = paramList2.get(i);
                    lup.setExternalprojectId(id);
                    LoginUserParameterFacade.create(lup, user.getEmail());
                }

                list list = new list(this.getServlet(), this.getParameter());
                list.navigate(context, request, response, out);
            }
            catch(Exception e) {
                log.warning(e.getMessage()+"/"+e.getCause()+"/"+e.getClass());
                request.setAttribute(constants.addExternalProject, "true");
                request.setAttribute(constants.externalProjectErrorMessage, wrongUrl);
                request.setAttribute(constants.externalProjectUrl, request.getParameter(constants.externalProjectUrl));
                request.setAttribute(constants.externalProjectName, request.getParameter(constants.externalProjectName));
                request.setAttribute(constants.externalProjectParameter, request.getParameter(constants.externalProjectParameter));
            }
        }
    }

    public static List<HiddenParameter> getHiddenParameterList(String parameter) {
        String parameterSplit[] = parameter.split(",");
        List<HiddenParameter> hpList = new ArrayList<HiddenParameter>();
        for(int i=0;i<parameterSplit.length;i++) {
            String request1 = parameterSplit[i];
            String values[] = request1.split("=");
            String key = values[0];
            String value = values[1];
            if(value.indexOf("$")!=-1) {
            }
            else {
                HiddenParameter hp = new HiddenParameter();
                hp.setName(key);
                hp.setValue(value);
                hpList.add(hp);
            }
        }
        return hpList;
    }

    public static List<LoginUserParameter> getLoginUserParameterList(String parameter) {
        String parameterSplit[] = parameter.split(",");
        List<LoginUserParameter> lupList = new ArrayList<LoginUserParameter>();
        for(int i=0;i<parameterSplit.length;i++) {
            String request1 = parameterSplit[i];
            String values[] = request1.split("=");
            String key = values[0];
            String value = values[1];
            if(value.indexOf("$")!=-1) {
                value = value.substring(1,value.length());
                LoginUserParameter lup = new LoginUserParameter();
                lup.setName(key);
                lup.setNameForUser(value);
                lupList.add(lup);
            }
        }
        return lupList;
    }
}